import { defineConfig } from 'vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import vue from '@vitejs/plugin-vue';
import { fileURLToPath } from 'node:url';

// https://vitejs.dev/config/
export default defineConfig({
    resolve: {
        //设置别名
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url)),
        },
    },
    plugins: [
        vue(),
        AutoImport({
            imports: ['vue'],
            // include: [/\d.ts$/],
            resolvers: [ElementPlusResolver()],
            dts: 'src/types/global/auto-imports.d.ts',
            dirs: ['src/types/api'],
        }),
        Components({
            dts: 'src/types/global/components.d.ts',
            dirs: ['src/components'],
            resolvers: [ElementPlusResolver({ importStyle: 'sass' })],
        }),
    ],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@use "@/style/element.scss" as *;`,
            },
        },
    },
    server: {
        port: 5174, //启动端口
        hmr: {
            host: '127.0.0.1',
            port: 8080,
        },
        open: true,
    },
    build: {
        terserOptions: {
            compress: {
                drop_console: true, // 打包后删除所有的console.log
            },
        },
        rollupOptions: {
            output: {
                assetFileNames: (assetInfo) => {
                    if (/\.(jpg|png|jpeg|gif)$/.test(assetInfo.name!)) {
                        // 匹配资源文件后缀
                        return `images/[name]-[hash].[ext]`; // 创建media文件夹存放匹配的资源文件,name为该文件的原名，hash为哈希值，ext为文件后缀名，以[name].[hash][ext]命名规则
                    }
                    return `assets/[name]-[hash].[ext]`; // 不匹配的资源文件存放至assets，以[name]-[hash].[ext]命名规则，注意两处的命名规则不同
                },
            },
        },
        sourcemap: false, // 生产环境一定要关闭
    },
});
